home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / Reference / DevCon / Orlando_1993 / Devcon93.4 / CAMD / examples / trans / boximage.c next >
Encoding:
C/C++ Source or Header  |  1993-01-01  |  6.4 KB  |  234 lines

  1. /* ============================================================================ *
  2.                               Talin's Application Shell
  3.                       box.c -- an image class for beveled boxes
  4.                                       By Talin.
  5.                                ©1991 The Dreamers Guild
  6.  * ============================================================================ *
  7.        This material is confidential -- do not distribute without authorization
  8.  * ============================================================================ */
  9.  
  10. #include <exec/types.h>
  11. #include <graphics/gfxmacros.h>
  12. #include <intuition/cghooks.h>
  13. #include <intuition/classusr.h>
  14. #include <intuition/classes.h>
  15. #include <intuition/imageclass.h>
  16.  
  17. #include <clib/exec_protos.h>
  18. #include <clib/graphics_protos.h>
  19. #include <clib/intuition_protos.h>
  20. #include <clib/gadtools_protos.h>
  21. #include <clib/utility_protos.h>
  22. #include <clib/realtime_protos.h>
  23. #include <clib/alib_protos.h>
  24. #include <clib/macros.h>
  25.  
  26. #include <pragmas/exec_pragmas.h>
  27. #include <pragmas/graphics_pragmas.h>
  28. #include <pragmas/intuition_pragmas.h>
  29. #include <pragmas/gadtools_pragmas.h>
  30. #include <pragmas/utility_pragmas.h>
  31. #include <pragmas/realtime_pragmas.h>
  32.  
  33. #include <stdlib.h>
  34. #include <string.h>
  35.  
  36. #include "boximage.h"
  37.  
  38. #define    IM(o)    ((struct Image *)(o))    /* transparent base    class */
  39.  
  40. extern struct Library    *IntuitionBase,
  41.                         *GfxBase,
  42.                         *UtilityBase;
  43.  
  44. struct BoxData {
  45.     UWORD                *glyph_data;            /* data for glyph in center        */
  46.     WORD                flags;                    /* flags for this image            */
  47. };
  48.  
  49. static drawFrame1( Class *cl, struct Image    *img, struct impDraw *msg );
  50.  
  51. static struct TagItem maskmap[] = {                /* used by PackBoolTags            */
  52.     BOX_Beveled,    BOX_BEVELED,
  53.     BOX_Filled,        BOX_FILLED,
  54.     BOX_ThickBevel,    BOX_THICK_BEVEL,
  55.     BOX_PushesIn,    BOX_PUSHES_IN,
  56.     BOX_LightFill,    BOX_LIGHT_FILL,
  57.     BOX_BorderFill, BOX_IN_BORDER,
  58.     TAG_DONE,        0
  59. };
  60.  
  61. static int setBoxAttrs( Class *cl, Object *o, struct opSet *msg )
  62. {    struct TagItem        *tags = msg->ops_AttrList,
  63.                         *tlist = tags;
  64.     struct Image        *im = (struct Image *)o,
  65.                         old_im;
  66.     struct BoxData        *bd = INST_DATA( cl, o ),
  67.                         old_bd;
  68.     int                    changes = 0;
  69.  
  70.     old_bd = *bd;                                /* save old state                */
  71.     old_im = *im;
  72.  
  73.         /* pack all the boolean tags into the flags field */
  74.  
  75.     bd->flags = PackBoolTags(bd->flags,tags,maskmap);
  76.  
  77.         /* iterate through the other tags and setup what values are needed */
  78.  
  79.     while (tags = NextTagItem(&tlist))
  80.     {    switch (tags->ti_Tag) {
  81.         case BOX_GlyphData: bd->glyph_data = (void *)tags->ti_Data; break;
  82.         case BOX_GlyphWidth: im->PlanePick = tags->ti_Data; break;
  83.         case BOX_GlyphHeight: im->PlaneOnOff = tags->ti_Data; break;
  84.         }
  85.     }
  86.  
  87.     if ( memcmp(bd,&old_bd,sizeof *bd) || memcmp(im,&old_im,sizeof *im) )
  88.     {    changes    |= 1;
  89.     }
  90.     return changes;
  91. }
  92.  
  93. static long __asm __saveds ourhook (
  94.     register __a0 Class *cl,
  95.     register __a1 struct impDraw *msg,
  96.     register __a2 struct Image *img )
  97. {    struct Image        *ni;
  98.     struct BoxData        *bd;
  99.  
  100.     switch ( msg->MethodID ) {
  101.     case IM_DRAW:                                /* draw    with state                */
  102.     case IM_DRAWFRAME:                            /* special case    of draw            */
  103.         return ( (long)drawFrame1( cl, img, (struct impDraw *)msg ) );
  104.  
  105.     case IM_HITTEST:                            /* return "TRUE" for hit test    */
  106.     case IM_HITFRAME:
  107.         return TRUE;
  108.  
  109. #if 0
  110.     case IM_FRAMEBOX:
  111.     case IM_ERASE:
  112.     case IM_ERASEFRAME:
  113. #endif
  114.  
  115.     default:
  116.         return (long) DoSuperMethodA( cl, (Object *)img, (Msg)msg );
  117.  
  118.     case OM_NEW:
  119.         if (!(ni = (struct Image *) DoSuperMethodA( cl, (Object *)img, (Msg)msg )))
  120.             return NULL;
  121.         bd = INST_DATA( cl, (Object *)ni );
  122.         bd->flags = BOX_BEVELED;
  123.         setBoxAttrs( cl, (Object *)ni, (struct opSet *)msg );
  124.         return (long)ni;
  125.     }
  126.     return 0;
  127. }
  128.  
  129. static drawFrame1( Class *cl, struct Image    *img, struct impDraw *msg )
  130. {    struct BoxImage        *bi = (struct BoxImage *)_OBJECT(img);
  131.     struct IBox            box;
  132.     UWORD                *pens;                    /* pen spec    array                */
  133.     UWORD                ulpen,                    /* upper left                    */
  134.                         lrpen,                    /* lower right                    */
  135.                         fillpen,                /* filled area                    */
  136.                         glyphpen;                /* glyph                         */
  137.     WORD                hthick;
  138.     WORD                flags = bi->flags;
  139.     struct RastPort        *rp = msg->imp_RPort;
  140.  
  141.     hthick = (flags & BOX_THICK_BEVEL) ? 2 : 1;
  142.  
  143.     /* let's be sure that we were passed a DrawInfo */
  144.  
  145.     pens = msg->imp_DrInfo->dri_Pens;
  146.     SetupImageIBox(img,(struct impDraw *)msg,&box);
  147.     glyphpen = pens[ textPen ];
  148.  
  149.     switch ( msg->imp_State    ) {
  150.     case IDS_SELECTED:
  151.     case IDS_INACTIVESELECTED:
  152.         if (flags & BOX_PUSHES_IN)
  153.         {    ulpen   = pens[    shadowPen ];
  154.             lrpen   = pens[    shinePen ];
  155.         }
  156.         else
  157.         {    ulpen   = pens[    shinePen ];
  158.             lrpen   = pens[    shadowPen ];
  159.         }
  160.         if (flags & BOX_LIGHT_FILL) fillpen = pens[shinePen];
  161.         else fillpen = pens[ !(flags & BOX_IN_BORDER) || msg->imp_State >=IDS_INACTIVENORMAL ? hifillPen : backgroundPen ];
  162.         break;
  163.  
  164.     case IDS_NORMAL:
  165.     case IDS_DISABLED:
  166.     case IDS_INACTIVENORMAL:
  167.     case IDS_INACTIVEDISABLED:
  168.     default:
  169.         ulpen   = pens[    shinePen ];
  170.         lrpen   = pens[    shadowPen ];
  171.         fillpen = pens[ !(flags & BOX_IN_BORDER) || msg->imp_State >= IDS_INACTIVENORMAL ? backgroundPen : hifillPen ];
  172.         break;
  173.     }
  174.  
  175.     rp->Mask = (UBYTE)~0;
  176.     SetDrMd(rp,    JAM1);
  177.  
  178.     if (hthick > 1) DrawBevel(rp,&box,ulpen,lrpen);
  179.     else DrawThinBevel(rp,&box,ulpen,lrpen);
  180.  
  181.     if (flags & BOX_FILLED && box.Width > (hthick<<1) && (box.Height > 2))
  182.     {
  183.         BNDRYOFF( rp );
  184.         SetAfPt(rp,    NULL, 0);
  185.         SetAPen(rp,    fillpen);
  186.  
  187.         RectFill( rp, box.Left+hthick, box.Top+1,
  188.             box.Left+box.Width-1-hthick, box.Top+box.Height-2 );
  189.     }
  190.  
  191.         /* if the plane pointer is filled in, then it means that there is
  192.             a Glyph to be drawn. PlanePick and PlaneOnOff are used to
  193.             indicate the size of the glyph
  194.         */
  195.  
  196.     if (bi->glyph_data)
  197.     {    int                w = img->PlanePick,
  198.                         h = img->PlaneOnOff;
  199.  
  200.         SetAPen(rp,glyphpen);
  201.         BltTemplate(
  202.             (void *)bi->glyph_data,0,((w+15)>>4)<<1,
  203.             rp,box.Left + (box.Width - w)/2,box.Top + (box.Height - h)/2,
  204.             w,h );
  205.     }
  206.  
  207.     return NULL;
  208. }
  209.  
  210. /* ============================================================================ *
  211.  *    initBoxClass: create data used by all bevel boxes
  212.  * ============================================================================ */
  213.  
  214. Class *initBoxClass(void)
  215. {    Class *cl;
  216.  
  217.     if (cl = MakeClass(                            /* Ask Intuition to make it        */
  218.         NULL,                                     /* Class ID    (none)                */
  219.         "imageclass", NULL,                        /* superclass is public, tho...    */
  220.         sizeof (struct BoxData),                /* Size of instance data        */
  221.         0 ))                                    /* No flags...                    */
  222.     {    cl->cl_Dispatcher.h_Entry =    (void *)ourhook;
  223.         cl->cl_Dispatcher.h_SubEntry = NULL;
  224.         cl->cl_Dispatcher.h_Data = 0L;
  225.     }
  226.     return cl;
  227. }
  228.  
  229.     /* This undoes initBoxClass */
  230.  
  231. LONG freeBoxClass(Class *cl)
  232. {    return (LONG)FreeClass(cl);
  233. }
  234.